﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace K3R.Data {
	public class Connection : IDisposable {
		protected string _connString = "";
		protected SqlConnection _conn = null;
		protected SqlCommand _cmd = null;
		protected CommandBehavior _behavior = CommandBehavior.Default;

		public SqlParameterCollection Parameters {
			get {
				SqlParameterCollection col = null;
				if (_cmd != null)
					col = _cmd.Parameters;
				return col;
			}
		}

		public Connection(string connString, string cmdText, CommandBehavior behavior, SqlParameter[] parameters) {
			_connString = connString;
			_conn = new SqlConnection(_connString);
			_cmd = new SqlCommand(cmdText, _conn);
			_cmd.CommandType = CommandType.StoredProcedure;
			_cmd.Parameters.AddRange(parameters);
			_behavior = behavior;
		}

		public Connection(string connString, string cmdText, CommandBehavior behavior, SqlParameter parameter) {
			_connString = connString;
			_conn = new SqlConnection(_connString);
			_cmd = new SqlCommand(cmdText, _conn);
			_cmd.CommandType = CommandType.StoredProcedure;
			_cmd.Parameters.Add(parameter);
			_behavior = behavior;
		}

		public Connection(string connString, string cmdText, SqlParameter[] parameters) {
			_connString = connString;
			_conn = new SqlConnection(_connString);
			_cmd = new SqlCommand(cmdText, _conn);
			_cmd.CommandType = CommandType.StoredProcedure;
			_cmd.Parameters.AddRange(parameters);
		}

		public Connection(string connString, string cmdText, SqlParameter parameter) {
			_connString = connString;
			_conn = new SqlConnection(_connString);
			_cmd = new SqlCommand(cmdText, _conn);
			_cmd.CommandType = CommandType.StoredProcedure;
			_cmd.Parameters.Add(parameter);
		}

		public Connection(string connString, string cmdText, CommandBehavior behavior) {
			_connString = connString;
			_conn = new SqlConnection(_connString);
			_cmd = new SqlCommand(cmdText, _conn);
			_cmd.CommandType = CommandType.StoredProcedure;
			_behavior = behavior;
		}

		public Connection(string connString, string cmdText) {
			_connString = connString;
			_conn = new SqlConnection(_connString);
			_cmd = new SqlCommand(cmdText, _conn);
			_cmd.CommandType = CommandType.StoredProcedure;
		}

		public Connection(string connString, string cmdText, bool isText) {
			_connString = connString;
			_conn = new SqlConnection(_connString);
			_cmd = new SqlCommand(cmdText, _conn);
			_cmd.CommandType = (isText ? CommandType.Text : CommandType.StoredProcedure);
		}

		public SqlParameter AddParameter(SqlParameter parameter) {
			SqlParameter ret = null;

			if (_cmd != null)
				ret = _cmd.Parameters.Add(parameter);

			return ret;
		}

		public void AddParameter(SqlParameter[] parameters) {
			if (_cmd != null)
				_cmd.Parameters.AddRange(parameters);
		}

		public SqlParameter AddParameter(string parameterName, object value) {
			return _cmd.Parameters.AddWithValue(parameterName, value);
		}

		public SqlParameter AddParameter(string parameterName, SqlDbType sqlDbType, ParameterDirection direction) {
			SqlParameter parameter = _cmd.Parameters.Add(parameterName, sqlDbType);
			parameter.Direction = direction;
			return parameter;
		}

		public SqlDataReader ExecuteReader(CommandBehavior behavior) {
			if (_conn.State == ConnectionState.Closed)
				_conn.Open();

			return _cmd.ExecuteReader(behavior);
		}

		public SqlDataReader ExecuteReader() {
			if (_conn.State == ConnectionState.Closed)
				_conn.Open();

			return ExecuteReader(_behavior);
		}

		public object ExecuteScalar() {
			if (_conn.State == ConnectionState.Closed)
				_conn.Open();

			return _cmd.ExecuteScalar();
		}

		public int ExecuteNonQuery() {
			if (_conn.State == ConnectionState.Closed)
				_conn.Open();

			return _cmd.ExecuteNonQuery();
		}

		#region IDisposable Members

		public void Dispose() {
			if (_cmd != null) {
				_cmd.Dispose();
				_cmd = null;
			}

			if (_conn != null) {
				_conn.Dispose();
				_conn = null;
			}
		}

		#endregion
	}
}
